using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using Trust.Casas.Model.Entities;
using Trust.Casas.Model.Migrations;
using Trust.Casas.Model.Support;

namespace Trust.Casas.Model
{
    public class TrustContext : DbContext
    {
        static TrustContext()
        {
            Database.SetInitializer<TrustContext>(new TrustContextInitializer());
        }

        public TrustContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<User> Users { get; set; }

        public DbSet<UserDetail> UserDetails { get; set; }

        public DbSet<AppSetting> AppSettings { get; set; }

        public DbSet<SearchType> SearchTypes { get; set; }

        public DbSet<BuildingType> BuildingTypes { get; set; }

        public DbSet<Region> Regions { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.ManyToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Add(new DateTime2Convention());
        }

        public static void Initialize()
        {
            using (TrustContext context = new TrustContext())
            {
                var p = context.AppSettings.FirstOrDefault();
                foreach (var item in GetScriptsToExecute())
                {
                    context.Database.ExecuteSqlCommand(item, new object[] { });
                }
                context.SaveChanges();
            }
        }

        private static IEnumerable<string> GetScriptsToExecute()
        {
            return new string[] { };
        }
    }


}